#include "stm32f10x.h"                  // Device header
#include "IR.h"
#include "GreeAircon.h"

void GreeAircon_Init(void) {
	IR_Send_Init();
}

void GreeAircon_On(void) {
	IR_SendHead();
	IR_SendStringBits("10010010010100000000010000001010010");
	IR_SendCat();
	IR_SendStringBits("00000000000001000000000000001111");
	IR_SendLongCat();
	IR_SendHead();
	IR_SendStringBits("10010010010100000000010000001110010");
	IR_SendCat();
	IR_SendStringBits("00000000000000000000000000001011");
	IR_SendLongCat();
}

void GreeAircon_Off(void) {
	IR_SendHead();
	IR_SendStringBits("10000010010100000000010000001010010");
	IR_SendCat();
	IR_SendStringBits("00000000000001000000000000001110");
	IR_SendLongCat();
	IR_SendHead();
	IR_SendStringBits("10000010010100000000010000001110010");
	IR_SendCat();
	IR_SendStringBits("00000000000000000000000000001010");
	IR_SendLongCat();
}


void Num2ReversedBinStr(char num,char *p,char len) {
	while(num > 0) {
		*p = num % 2 | 0x30;
		p++;
		len--;
		num /= 2;
	}
	while(len > 0) {
		*p = '0';
		p++;
		len--;
	}
}
#include <stdio.h>
#include <string.h>
#include "OLED.h"
void GreeAircon_SetStatus(unsigned char temperature,unsigned char fanMode) {
	char code1[64] = {0};
	char code2[64] = {0};
	char code3[64] = {0};
	char code4[64] = {0};
	char tempStr[5] = {0};
	char checkSum1[5] = {0};
	char checkSum2[5] = {0};
	char fanCode1[3] = {0};
	char fanCode2[5] = {0};
	unsigned char checkSum = 0;
	temperature -= 16;
	checkSum = 9 + temperature + 2 + 10;
	if(temperature > 15)
		temperature = 15;
	if(fanMode > 5)
		fanMode = 5;
	Num2ReversedBinStr(temperature,tempStr,4);
	Num2ReversedBinStr(checkSum%16,checkSum1,4);
	Num2ReversedBinStr(fanMode,fanCode2,4);
	if(fanMode < 3) {
		Num2ReversedBinStr(fanMode,fanCode1,2);
	} else {
		fanCode1[0] = '1';
		fanCode1[1] = '1';
		fanCode1[2] = 0;
	}
	checkSum = 9 + temperature + fanMode + 10;
	Num2ReversedBinStr(checkSum%16,checkSum2,4);
	sprintf(code1,"1001%2s00%4s00000000010000001010010",fanCode1,tempStr);
	sprintf(code2,"0000000000000100000000000000%4s",checkSum1);
	sprintf(code3,"1001%2s00%4s00000000010000001110010",fanCode1,tempStr);
	sprintf(code4,"00000000000000000000%-4s0000%4s",fanCode2,checkSum2);
	IR_SendHead();
	IR_SendStringBits(code1);
	IR_SendCat();
	IR_SendStringBits(code2);
	IR_SendLongCat();
	IR_SendHead();
	IR_SendStringBits(code3);
	IR_SendCat();
	IR_SendStringBits(code4);
	IR_SendLongCat();
}
//9 11 0 0 0 5 10 35 = 32 + 2 +1 = 100101
